<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 200px;
            height: 200px;
            background-color: red;
            position: absolute;
            top: 100px;
            left: 100px;

        }
    </style>
</head>
<body>
    <div class="box"></div>
    <script>
        let box=document.querySelector(".box")
        // console.log(box.offsetLeft)
        document.onkeydown=function(e){
            // console.log(e.keyCode)
            let code=e.keyCode
            switch(code){
                case 37:box.style.left=box.offsetLeft-5+"px";break;
                case 38:box.style.top=box.offsetTop-5+"px";break;
                case 39:box.style.left=box.offsetLeft+5+"px";break;
                case 40:box.style.top=box.offsetTop+5+"px";break;
            }
        }
    </script>
</body>
</html>